﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyServer.Common;
using System.Net;
using System.Net.Sockets;
using FrameWork.Log;
using FrameWork.Setting;
using System.ComponentModel;

namespace MyServer.TcpIP
{
    [SettingParentDefine]
    public class TcpIpConnectManager : ConnectManagerBase
    {

        #region========私有变量========

        /// <summary>
        /// 停止状态标识
        /// </summary>
        private bool IsStop = false;
        #endregion

        #region ==========属性==========

        /// <summary>
        /// 表示监听工作流
        /// </summary>
        public Task ListenTask { get; private set; }
        /// <summary>
        /// 表示与管理器关联的Tcp侦听器
        /// </summary>
        public TcpListener Listener { get; private set; }
        /// <summary>
        /// 获取或设置连接管理器侦听连接的频率
        /// </summary>
        [SettingItemDefine(SettingValueType.Int32,10)]
        [Browsable(true)]
        [Category("常规")]
        [DisplayName("侦听频率")]
        [Description("表示TcpIp连接管理器侦听新连接的频率")]
        public int ListenRate { get; set; }

        #endregion

        #region========私有函数========
        
        private void DoListen()
        {
            PrintLog(LogType.Info, "监听线程已启动！");
            while (!IsStop)//反复循环取得连接
            {
                if (Listener.Pending())
                {
                    TcpClient c = Listener.AcceptTcpClient();
                    ConnectEventArgs e = new ConnectEventArgs(c);
                    base.OnNewConnectionJoin(this,e);
                    if (!e.Canceled)
                        this.ClientList.Add(c);
                }
                else
                {
                    System.Threading.Thread.Sleep(ListenRate);
                }
            }
            PrintLog(LogType.Info, "监听线程已停止！");
        }
        
        #endregion

        #region========公有函数========

        /// <summary>
        /// 开始监听连接请求
        /// </summary>
        public override void StartListen()
        {
            PrintLog(LogType.Info, "正在启动Tcp连接管理器...");
            PrintLog(LogType.Info, "正在启动连接监听...");
            Listener.Start();
            if (ListenTask.Status != TaskStatus.Running)
            {
                PrintLog(LogType.Info, "正在重构主工作线程...");
                ListenTask = new Task(DoListen);
                PrintLog(LogType.Info, "正在启动主工作线程...");
                ListenTask.Start();
            }
            else
            {
                PrintLog(LogType.Waring, "主工作线程未终止，将取消重构！");
            }
            PrintLog(LogType.Info, "启动Tcp连接管理器成功！");
        }

        public override void Stop()
        {
            PrintLog(LogType.Info, "正在停止Tcp连接管理器...");
            IsStop = true;
            PrintLog(LogType.Info, "正在等待主工作线程完成...");
            while (ListenTask.Status == TaskStatus.Running)
            {
                System.Threading.Thread.Sleep(10);
            }
            PrintLog(LogType.Info, "正在停止连接侦听...");
            Listener.Stop();
            PrintLog(LogType.Info, "Tcp连接管理器停止成功！");
        }

        #endregion

        #region========构造函数========
        /// <summary>
        /// 通过指定的本地终结点信息初始化管理器
        /// </summary>
        /// <param name="EndPoint"></param>
        public TcpIpConnectManager(IPEndPoint EndPoint, TcpIpCenter Center)
            : base(Center)
        {
            PrintLog(LogType.Info, "正在初始化Tcp收发中心...");
            this.Listener = new TcpListener(EndPoint);
            this.ListenRate = 10;
            this.ListenTask = new Task(DoListen);
            PrintLog(LogType.Info, "Tcp收发中心初始化成功！");
        }
        #endregion

    }
}
